<?php
/*
class ss_core_settings{
	static private $instance;
	
	public function __construct($ss){
		self::$instance=$this;
		foreach(array('error','dao','server') as $key) $this->{$key}=(object) array();
		foreach($ss as $key=>$val){
			$this->{$key}=$val;
		}
	}
	
	static public function instance(){
		return self::$instance;
	}
}
*/
class configFileDoesNotExist extends Exception{}

interface ss_core_configInterface extends arrayaccess{}

class ss_core_config implements ss_core_configInterface{
	protected $base;
	protected $data=array();
	
	public function __construct($data=null){
		if(is_array($data)) $this->setData($data);
	}
	
	public function merge(ss_core_config $base){
		$this->base=$base;
		$this->data=array_replace_recursive($this->base->getData(),$this->data);
		return $this;
	}
	
	public function setData($data){
		$this->data=$data;
		return $this;
	}
	
	public function getData(){
		return $this->data;
	}
	
    public function offsetSet($offset, $value) {
        $this->data[$offset] = $value;
    }
	
    public function offsetExists($offset) {
        return isset($this->data[$offset]);
    }
	
    public function offsetUnset($offset) {
        unset($this->data[$offset]);
    }
	
    public function offsetGet($offset) {
        return isset($this->data[$offset]) ? $this->data[$offset] : null;
    }
	
	public function toJSON($offset){
		return json_encode($this->data[$offset]);
	}
}

class ss_core_configYaml extends ss_core_config{
	protected $filebase;
	protected $cachePrefix='.cache.php';
	protected $confPrefix='.yml';
	protected $cli=false;
	
	public function __construct($basename=null,$path='',$cli=false){
		$this->cli=$cli;
		if(!is_null($basename)) {
			try{
				$this->load($basename.($this->cli?'.cli':''),$path);
			} catch(Exception $e){
				$this->load($basename,$path);
			}
		}
	}
	
	private function load($basename,$path=''){
		//$this->basename=$basename;
		$this->filebase=$path.$basename;
		if(file_exists($this->filebase.$this->confPrefix)) {
			if($this->isCached()){
				$this->setData(unserialize(file_get_contents($this->filebase.$this->cachePrefix)));
			} else {
				$data=Spyc::YAMLLoad($this->filebase.$this->confPrefix);
				file_put_contents($this->filebase.$this->cachePrefix,serialize($data));
				$this->setData($data);
			}
		} else {
			throw new configFileDoesNotExist($this->filebase.$this->confPrefix);
		}
		return $this;
	}
	
	public function getDate(){
		
	}
	
	public function isCached(){
		if(file_exists($this->filebase.$this->cachePrefix)){
			if(filemtime($this->filebase.$this->cachePrefix)>=filemtime($this->filebase.$this->confPrefix)){
				return true;
			} else {
				unlink($this->filebase.$this->cachePrefix);
				return false;
			}
		}
	}
}

class ss_core_serverConfig extends ss_core_configYaml{
	
}

interface ss_core_injectorInterface{
	public function create($classname);
	public function get($classname);
}


class ss_core_decorator{
	protected $d_obj;
	
	public function __construct($d_obj){
		$this->d_obj=$d_obj;
	}
	
	function __call($method, $args){
		return call_user_func_array(array($this->d_obj, $method), $args);
	}
}

class ss_util{
	public static function toSlug($string,$space="-") {  
  
	if (function_exists('iconv')) {  
	    $string = @iconv('UTF-8', 'ASCII//TRANSLIT', $string);  
	}
	$string = preg_replace("/[^a-zA-Z0-9 -]/", "", $string);  
	$string = strtolower($string);  
	$string = str_replace(" ", $space, $string);  
   
	return $string;  
   
 } 
}
?>